<?php 
/**
 * 添加分类的操作
 * @return string
 */
function addCate(){
	$PdoMySQL=new PdoMySQL;
	@$arr=$_POST;


	$path="./uploads";
	// var_dump($arr);
	$uploadFiles=uploadFile($path);


	@$arr[bPicPath] = $uploadFiles[0]['name'];
	@$arr[mPicPath] = $uploadFiles[1]['name'];


	if($PdoMySQL->add($arr,"bac_cate")){
		$mes="分类添加成功!<br/><a href='addCate.php'>继续添加</a>|<a href='listCate.php'>查看分类</a>";
	}else{
		$mes="分类添加失败！<br/><a href='addCate.php'>重新添加</a>|<a href='listCate.php'>查看分类</a>";
	}
	return $mes;
}

/**
 * 根据ID得到指定分类信息
 * @param int $id
 * @return array
 */
function getCateById($id){
	
	$PdoMySQL=new PdoMySQL;
	$tables = "bac_cate";
	
	$row = $PdoMySQL->findById($tables, $id, 'id, cName, bPicPath, mPicPath');
	return $row;
}

/**
 * 修改分类的操作
 * @param string $where
 * @return string
 */
function editCate($id){
	$PdoMySQL=new PdoMySQL;
	$table = "bac_cate";
	$arr=$_POST;

	$row = $PdoMySQL->findById($table, $id, 'id, cName, bPicPath, mPicPath');

	if ($row['cName'] == $arr['cName']) {
		unset($arr['cName']); 
	}

	$path="./uploads";
	$uploadFiles=uploadFile($path);
	if ($uploadFiles &&is_array($uploadFiles)) {
		@$arr[bPicPath] = $uploadFiles[0]['name'];
		@$arr[mPicPath] = $uploadFiles[1]['name'];
	} 
	
	if ($arr &&is_array($arr)) {
		if($PdoMySQL->update($arr,$table, "id={$id}",'','')){
			$mes="分类修改成功!<br/><a href='listCate.php'>查看分类</a>";
		}else{
			$mes="分类修改失败!<br/><a href='listCate.php'>重新修改</a>";
		}
	} else{
		$mes="分类修改成功!<br/><a href='listCate.php'>查看分类</a>";
	}
	
	
	return $mes;
}

/**
 *删除分类
 * @param string $where
 * @return string
 */
function delCate($id){

	$res=checkProExist($id);
	if(!$res){
		$PdoMySQL=new PdoMySQL;
		$table = "bac_cate";
		
		if($PdoMySQL->delete($table,"id={$id}")){
			$mes="分类删除成功!<br/><a href='listCate.php'>查看分类</a>|<a href='addCate.php'>添加分类</a>";
		}else{
			$mes="删除失败！<br/><a href='listCate.php'>请重新操作</a>";
		}
	}else{
		alertMes("不能删除分类，请先删除该分类下的项目", "listPro.php");
	}
	return $mes;
}
/**
 * 检查分类是否存在
 * @param 
 * @return Ambigous <multitype:, multitype:>
 */
function checkCate(){
	$cateName = $_POST['cateName'];
	$PdoMySQL=new PdoMySQL;
	$sql="select id,cname from bac_cate where cname='{$cateName}'";
	// $flag =false; 
	$count = 0;
	$count = count($PdoMySQL->getAll($sql));
	echo $count > 0?"该分类已存在":"";
}
/**
 * 得到所有分类
 * @return array
 */
function getAllCate(){
	$PdoMySQL=new PdoMySQL;
	$table = "bac_cate";
	$rows=$PdoMySQL->find($table,null,'*',null,null,null,null);
	return $rows;
}
function getCateByPage($page,$pageSize=2){
	$PdoMySQL=new PdoMySQL;
	$tables = "bac_cate";
    global $totalRows;
	$totalRows=count($PdoMySQL->find($tables,null,'*',null,null,null,null));
	global $totalPage;
	$totalPage=ceil($totalRows/$pageSize);
	if($page<1||$page==null||!is_numeric($page)){
		$page=1;
	}

	if($page>=$totalPage)$page=$totalPage;
	$offset=($page-1)*$pageSize;

	$rows=$PdoMySQL->find($tables,null,'*',null,null,null,array($offset,$pageSize));
	return $rows;

}


